package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * 套餐管理
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;
    //查询所有套餐
    @RequestMapping("/getAllSetmeal")
    public Result getAllSetmeal(){
        try{
            String getAllSetmeal = jedisPool.getResource().get("getAllSetmeal");
            List<Setmeal> list=null;
            if (getAllSetmeal!=null && getAllSetmeal.length()>0){
             list = setmealService.findAll();
                    //将查询套餐存入redis中
                String json = JSON.toJSON(list).toString();
                jedisPool.getResource().setex(getAllSetmeal,60*60*60*24*30,json);
                System.out.println("查询套餐存入到redis中了");
            }

            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    //根据套餐ID查询套餐详情（套餐基本信息、套餐对应的检查组信息、检查组对应的检查项信息）
    @RequestMapping("/findById")
    public Result findById(int id){
        try{
            Setmeal setmeal = setmealService.findById(id);
            if (setmeal!=null){
                //将套餐详情存入redis中
                String json = JSON.toJSON(setmeal).toString();
                jedisPool.getResource().setex(String.valueOf(setmeal),60*60*60*24*30,json);
                System.out.println("套餐详情数据存入到redis中");
            }
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
